package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodCilent;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-09
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodCilent vodCilent;

    //1. 根据课程id 删除小结
    //todo 删除 同时删除对应阿里云视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {

        //根据课程id 查询出所有的视频id
        QueryWrapper<EduVideo>  videoQueryWrapper = new QueryWrapper<>();
        //条件
        videoQueryWrapper.eq("course_id",courseId);
        videoQueryWrapper.select("video_source_id");

        List<EduVideo> eduVideoList = baseMapper.selectList(videoQueryWrapper);
        //得到所有视频列表的云端原始视频id
        List<String> videoIds = new ArrayList<>();
        for (int i = 0; i < eduVideoList.size(); i++) {
            EduVideo video = eduVideoList.get(i);
            String videoSourceId = video.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                videoIds.add(videoSourceId);
            }
        }
        if(videoIds.size()>0) {
            vodCilent.deleteBatch(videoIds);
        }


        QueryWrapper<EduVideo>  wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);      // 数据库字段
        baseMapper.delete(wrapper);
    }
}
